package com.topisv.tms.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.topisv.tms.entity.Function;
import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.Permission;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.service.FunctionService;
import com.topisv.tms.service.PermissionService;
import com.topisv.tms.web.base.BaseDataTableController;

/**
 * 功能管理的Controller, 使用Restful风格的Urls:
 * 
 * List page     : GET /permission/
 * Create page   : GET /permission/create
 * Create action : POST /permission/create
 * Update page   : GET /permission/update/{id}
 * Update action : POST /permission/update
 * Delete action : GET /permission/delete/{id}
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/permission")
public class PermissionController extends BaseDataTableController{

	private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
	static {
		sortTypes.put("auto", "自动");
		sortTypes.put("showname", "账号");
	}

	@Autowired
	private PermissionService permissionService;
	
	@Autowired
	private FunctionService functionService;

	@RequestMapping(value = "v2_show")
	public String list() {
		return "permission/permissionList";
	}
	
	@RequestMapping(value = "page",method = RequestMethod.POST)
	public void page(@RequestBody JSONParam[] paramJson, HttpServletRequest request,HttpServletResponse response){
		try {
			// 客户端参数
			QueryParamHolder paramHolder = collectParam(paramJson);
			Page<Permission> page = permissionService.getPermission(paramHolder);
			// 封装查询结果
			response.getWriter().print(bulidJSON(page));
		} catch (Exception e) {
			
		}
	}
	@RequestMapping(value = "v2_create", method = RequestMethod.GET)
	public String createForm(Model model) {
		Permission perm = new Permission();
		Function function = new Function();
		perm.setFunction(function);
		List<Function> functions = functionService.getMenu("1");
		model.addAttribute("permission", perm);
		model.addAttribute("functions", functions);
		model.addAttribute("action", "create");
		return "permission/permissionForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid Permission permission, RedirectAttributes redirectAttributes) {
		permissionService.savePermission(permission);
		redirectAttributes.addFlashAttribute("message", "创建权限成功");
		return "redirect:/permission/v2_show";
	}

	@RequestMapping(value = "v2_update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		List<Function> functions = functionService.getMenu("1");
		model.addAttribute("functions", functions);
		model.addAttribute("permission", permissionService.getPermission(id));
		model.addAttribute("action", "update");
		return "permission/permissionForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("preloadBaseUsers") Permission permission, RedirectAttributes redirectAttributes) {
		permissionService.savePermission(permission);
		redirectAttributes.addFlashAttribute("message", "更新权限成功");
		return "redirect:/permission/v2_show";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		permissionService.deletePermission(id);
		redirectAttributes.addFlashAttribute("message", "删除权限成功");
		return "redirect:/permission/v2_show";
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadPermission")
	public Permission getRole(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			return permissionService.getPermission(id);
		}
		return null;
	}

}
